package com.es.commons.intercepter;

import com.es.api.enums.BusinessTypeEnum;
import com.es.system.entity.SysOperateLogDO;
import com.es.system.service.SysOperateLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;

/**
 * 描述:
 * Old-AgeCareSystem-
 *
 * @author Derek
 * @create 2021-12-06 11:15
 */
@Component
@Slf4j
public class LogInterceptor implements HandlerInterceptor {

    @Autowired
    private SysOperateLogService service;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //
        SysOperateLogDO sysOperateLog = new SysOperateLogDO();
        //
        String uri = request.getRequestURI();
        log.info("uri:{}",uri);
        sysOperateLog.setBusinessType(BusinessTypeEnum.LOGIN.getCode());
        sysOperateLog.setOperateName("系统管理");
        sysOperateLog.setOperatorName("admin");
        sysOperateLog.setRequestMethod(request.getMethod());
        sysOperateLog.setClientIp(request.getRemoteAddr());
        sysOperateLog.setHeads(request.getRemoteHost());
        sysOperateLog.setClientOs(request.getHeader("sec-ch-ua-platform"));
        sysOperateLog.setClientBrowser(request.getHeader("sec-ch-ua"));
        sysOperateLog.setOperateUrl(uri);
        sysOperateLog.setCreateTime(LocalDateTime.now());
        //
        sysOperateLog.setDescription("uri");
        service.save(sysOperateLog);
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }
}
